﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SellProductsForm
{
    public partial class frmSell : UTLBASEFORM.MDICHILD
    {
        UTLCOMMAND.UTLCommand utcommand = new UTLCOMMAND.UTLCommand();
        DataTable dataPro;
        TSell tSell = new TSell();

        public frmSell()
        {
            InitializeComponent();
        }

        private void SetupMenuBar()
        {
            ShowBTN1("Bán", ImageNameCheck_Ok);
            ShowBTN2("Xóa", ImageNameDelete);
            // ShowBTN3("In hóa đơn", ImageNameEdit);
        }
        private void frmSell_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            SetDefaultColorList(lvProduct);
            SetDefaultColorList(lvSeedL);
            SetDefaultColorList(lvSeedM);

            NumerTextBoxWithSeparator = txtPriceShell;
            NumerTextBoxWithSeparator = txtTienCong;
            NumerTextBoxWithSeparator = txtPriceL;
            LoadEmployee();

        }
        public void LoadEmployee()
        {
            DataTable dt = tSell.GetEmployee();
            cmbNV.DataSource = dt;
            cmbNV.DisplayMember = "FullName";
            cmbNV.ValueMember = "ID";
            AutoCompleteStringCollection collec = new AutoCompleteStringCollection();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                collec.Add(dt.Rows[i]["FullName"].ToString());
            }
            cmbNV.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cmbNV.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cmbNV.AutoCompleteCustomSource = collec;

        }
        public void LoadAutoEmploy()
        {
        }
        public void ReSetForm()
        {
            txtProduct.Text = "";
            txtWShell.Text = "";
            txtMaterial.Text = "";
            txtPriceShell.Text = "";
            txtPriceUnit.Text = "";
            txtPriceM.Text = "";
            txtPriceL.Text = "";
            txtPriceProduct.Text = "";
            txtPriceUnit.Text = "";
        }

        private void SetShowWL(string str)
        {
            string[] tam = str.Split('.');
            string[] w = new string[] { "", "", "", "" };
            if (tam.Length != 0)
            {
                int num = tam[0].Length;
                if (num > 4) num = 4;
                for (int i = 0; i < num; i++)
                    w[i] = tam[0].Substring(i, 1);
                if (num == 1)
                {
                    txtW.Luong = "";
                    txtW.Chi = "";
                    txtW.Phan = "";
                    txtW.Li = w[0].ToString();
                }
                else if (num == 2)
                {
                    txtW.Luong = "";
                    txtW.Chi = "";
                    txtW.Phan = w[0].ToString();
                    txtW.Li = w[1].ToString();
                }
                else if (num == 3)
                {
                    txtW.Luong = "";
                    txtW.Chi = w[0].ToString();
                    txtW.Phan = w[1].ToString();
                    txtW.Li = w[2].ToString();
                }
                else if (num == 4)
                {

                    txtW.Luong = w[0].ToString();
                    txtW.Chi = w[1].ToString();
                    txtW.Phan = w[2].ToString();
                    txtW.Li = w[3].ToString();
                }


                if (tam.Length > 1)
                    txtW.PhanLe = tam[1].ToString();
            }

        }
        public void CommitValue()
        {
            if (txtPriceL.Text.Trim() == "") txtPriceL.Text = "0";
            if (txtPriceShell.Text.Trim() == "") txtPriceShell.Text = "0";
            if (txtTienCong.Text.Trim() == "") txtTienCong.Text = "0";
            if (txtPriceM.Text == string.Empty) txtPriceM.Text = "0";
        }
        private void LoadText(DataTable data)
        {
            if (!bool.Parse(data.Rows[0]["Change"].ToString()))
            {
                txtPriceUnit.Enabled = true;
                txtPriceShell.Enabled = false;
                txtPriceUnit.Text = utcommand.FormatMoney(data.Rows[0]["PriceUnit"].ToString().Trim(), "VND");
                //   float price=float.Parse(data.Rows[0]["PriceUnit"].ToString().Trim()) * float.Parse(data.Rows[0]["Weight"].ToString());
                txtPriceShell.Text = "0";//= utcommand.FormatMoney(String.Format("{0:0.00}", price),"VND"); 
            }
            else
            {
                txtPriceUnit.Enabled = false;
                txtPriceUnit.Text = "<Giá cố định>";
                txtPriceShell.Enabled = true;
                txtPriceShell.Text = utcommand.FormatMoney(data.Rows[0]["Price"].ToString().Trim(), "VND");
            }
            if (data.Rows[0]["UnitID"].ToString().Trim() == "1")
            {
                txtWShell.Visible = true;
                txtW.Visible = false;

                txtWShell.Text = data.Rows[0]["Weight"].ToString().Trim();
            }
            else
            {
                txtWShell.Visible = false;
                txtW.Visible = true;
                SetShowWL(data.Rows[0]["Weight"].ToString().Trim());
            }
            txtProduct.Text = data.Rows[0]["Name"].ToString().Trim();
            txtWShell.Text = data.Rows[0]["Weight"].ToString().Trim();
            txtMaterial.Text = data.Rows[0]["GoldType"].ToString().Trim();
            txtTienCong.Text = utcommand.FormatMoney(data.Rows[0]["TienCong"].ToString().Trim(), "VND");
            LoadSeed(int.Parse(data.Rows[0]["ID"].ToString()));

            //txtPriceShell.Text = data.Rows[0]["Price"].ToString();
            // txtWL.Text = data.Rows[0]["SizeL"].ToString().Trim();
            //txtPriceL.Text =utcommand.FormatMoney(data.Rows[0]["PriceL"].ToString().Trim(),"VND");
            // txtNumL.Text = data.Rows[0]["NumberSeedLarge"].ToString().Trim();

            // txtWM.Text = data.Rows[0]["SizeM"].ToString().Trim();
            //  txtPriceM.Text =utcommand.FormatMoney(data.Rows[0]["PriceM"].ToString().Trim(),"VND");
            //  txtNumM.Text = data.Rows[0]["NumberSeedSmall"].ToString();
            //  float priceP = float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) * int.Parse(txtNumL.Text) + float.Parse(utcommand.FormatString(txtPriceM.Text)) * int.Parse(txtNumM.Text);
            CommitValue();
            txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceM.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");

        }
        public string FormatWeight(string str)
        {
            string size = "";
            string[] tam = str.Trim().Split('.');
            string phanle;
            if (tam.Length > 1) phanle = tam[1];
            else phanle = "";

            switch (tam[0].Length)
            {
                case 1:
                    size = tam[0] + "L" + phanle;
                    break;
                case 2:
                    size = tam[0].Substring(0, 1) + "P"
                        + tam[0].Substring(1, 1) + "L" + phanle;
                    break;
                case 3:
                    size = tam[0].Substring(0, 1) + "C"
                        + tam[0].Substring(1, 1) + "P" +
                        tam[0].Substring(2, 1) + "L" + phanle;
                    break;
                case 4:
                    size = tam[0].Substring(0, 1) + "L" +
                        tam[0].Substring(1, 1) + "C"
                        + tam[0].Substring(2, 1) + "P" +
                        tam[0].Substring(3, 1) + "L" + phanle;
                    break;

            }
            return size;
        }
        public void LoadSeed(int id)
        {
            DataTable dataSeed = tSell.SelectSeedByProduct(id);
            float priceM = 0;
            float priceL = 0;
            lvSeedL.Items.Clear();
            lvSeedM.Items.Clear();
            for (int j = 0; j < dataSeed.Rows.Count; j++)
            {
                if (dataSeed.Rows[j]["Name"].ToString().Trim() == "Hạt Tấm")
                {

                    string size = "";
                    size = FormatWeight(dataSeed.Rows[j]["Size"].ToString());
                    ListViewItem item = new ListViewItem(dataSeed.Rows[j]["Type"].ToString() + ":" + size);
                    lvSeedM.Items.Add(item);
                    // item.SubItems.Add(stt.ToString());
                    item.SubItems.Add(dataSeed.Rows[j]["Num"].ToString());
                    priceM += float.Parse(dataSeed.Rows[j]["Price"].ToString()) * int.Parse(dataSeed.Rows[j]["Num"].ToString());
                }
                else
                {
                    string size = "";
                    size = FormatWeight(dataSeed.Rows[j]["Size"].ToString());
                    ListViewItem item = new ListViewItem(dataSeed.Rows[j]["Type"].ToString() + ":" + size);
                    lvSeedL.Items.Add(item);
                    // item.SubItems.Add(stt.ToString());
                    item.SubItems.Add(dataSeed.Rows[j]["Num"].ToString());
                    priceL += float.Parse(dataSeed.Rows[j]["Price"].ToString()) * int.Parse(dataSeed.Rows[j]["Num"].ToString());

                }
            }
            txtPriceL.Text = utcommand.FormatMoney(priceM.ToString(), "VND");
            txtPriceM.Text = utcommand.FormatMoney(priceL.ToString(), "VND");

        }
        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            string strTxt = string.Empty;
            strTxt = txtBarcode.Text.Trim();
            if (strTxt.StartsWith("*"))
            {
                strTxt = strTxt.Substring(1);
            }
            if (strTxt.EndsWith("*"))
            {
                strTxt = strTxt.Substring(0, strTxt.Length - 1);
            }
            txtBarcode.Text = strTxt;
            if (txtBarcode.Text.Trim().Length < 6)
                ReSetForm();
            else 
                if (txtBarcode.Text.Trim().Length == 6)
            {
                dataPro = tSell.LoadProduct(txtBarcode.Text.Trim());
                if (dataPro.Rows.Count > 0)
                    if (dataPro.Rows[0]["Status"].ToString().Trim() == "1" || dataPro.Rows[0]["Status"].ToString().Trim() == "0")
                    {
                        ReSetForm();
                        LoadText(dataPro);
                    }
            }

        }
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (txtBarcode.Text == string.Empty || txtProduct.Text == string.Empty || txtPriceProduct.Text == string.Empty)
            {
                return;
            }

            //lvProduct.Items.Clear();
            int flag = 0;
            int i = 0;
            for (i = 0; i < lvProduct.Items.Count; i++)
                if (dataPro.Rows[0]["Barcode"].ToString().Trim() == lvProduct.Items[i].SubItems[1].Text.ToString().ToUpper().Trim())
                {
                    flag = 1;
                    break;
                }
            if (flag == 0)
            {
                //  NumPro++;
                i++;
                ListViewItem item = new ListViewItem(i.ToString());
                lvProduct.Items.Add(item);

                item.SubItems.Add(txtBarcode.Text);

                item.SubItems.Add(txtProduct.Text.ToString());
                float price = float.Parse(utcommand.FormatString(txtPriceProduct.Text.ToString()));
                item.SubItems.Add(utcommand.FormatMoney(String.Format("{0:0.00}", price), "VND"));
                txtTotalPrice.Text = utcommand.FormatMoney(String.Format("{0:0.00}", (float.Parse(txtTotalPrice.Text) + price)), "VND");
                //  if (i % 2 == 0)
                //{
                //   item.BackColor = Color.AliceBlue;
                //    //return ;
                //}
                //else
                //  {
                //      item.BackColor = Color.AntiqueWhite;
                //      //return Brushes.Beige;
                //  }
                SetDefaultColorList(lvProduct);
                SetDefaultColorList(lvSeedL);
                SetDefaultColorList(lvSeedM);

            }
        }
        public infoTally_out SetTally()
        {
            infoTally_out info = new infoTally_out();
            info.DateCreate = DateTime.Now;

            //MainProject.MainMDIPaarent formmain = new MainProject.MainMDIPaarent();
            //UTLBASEFORM.MDIPARENT d = new UTLBASEFORM.MDIPARENT();

            //  info.MemberID = UserLoginInfo.ID;
            info.MemberID = Int32.Parse(cmbNV.SelectedValue.ToString());
            info.CustomerID = 1;
            info.NumberProduct = lvProduct.Items.Count;
            info.MoneySum = float.Parse(utcommand.FormatString(txtTotalPrice.Text));
            info.Active = true;
            return info;
        }
        private int InsertTally_out()
        {
            int id = 0;
            infoTally_out info = SetTally();
            id = tSell.InsertTally(info);
            return id;

        }
        public void InsertDetail(int idTally)
        {
            infoDetailTally info;
            for (int i = 0; i < lvProduct.Items.Count; i++)
            {
                info = new infoDetailTally();
                info.TallyOutid = idTally;
                info.Barcode = lvProduct.Items[i].SubItems[1].Text.ToString();
                info.Price = float.Parse(utcommand.FormatString(lvProduct.Items[i].SubItems[3].Text.ToString()));
                tSell.InsertDetailTally(info);
                tSell.UpdateProduct(info.Barcode, 2);
            }
            // return tSell.InsertDetailTally(info);


        }
        public override void btn1_Click(object sender, EventArgs e)
        {
            if (lvProduct.Items.Count < 1)
                return;
            else
            {
                int id = InsertTally_out();
                InsertDetail(id);
                ReSetForm();
                lvProduct.Items.Clear();
                txtTotalPrice.Text = "";
                lvSeedL.Items.Clear();
                lvSeedM.Items.Clear();
            }
        }
        public override void btn2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lvProduct.Items.Count; i++)
                if (lvProduct.Items[i].Checked == true)
                {
                    //I show here the second field text (SubItems[1].Text) from the selected row(Items[i]) 
                    // MessageBox.Show(lvEmployee.Items[i].SubItems[2].Text);

                    float price = float.Parse(lvProduct.Items[i].SubItems[3].Text);
                    txtTotalPrice.Text = utcommand.FormatMoney(String.Format("{0:0.00}", (float.Parse(txtTotalPrice.Text) - price)), "VND");
                    lvProduct.Items[i].Remove();
                    // break;
                }
        }
        private void txtPriceL_TabIndexChanged(object sender, EventArgs e)
        {
            if (txtPriceL.Text.Length > 12)
                MessageBox.Show("Nhập lại giá hạt");
            if (txtPriceM.Text == "")
                txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");
            if (txtPriceL.Text == "")
                txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceM.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");
            if (txtPriceShell.Text == "")
                txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceM.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");
            if (txtPriceShell.Text != "" && txtPriceM.Text != "" && txtPriceL.Text != "")
                txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceM.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");

        }


        private void txtPriceShell_Leave(object sender, EventArgs e)
        {
            CommitValue();
            txtPriceShell.Text = utcommand.FormatMoney(txtPriceShell.Text, "VND");
            txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceM.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");

        }

        private void txtPriceL_Leave(object sender, EventArgs e)
        {
            CommitValue();
            txtPriceL.Text = utcommand.FormatMoney(txtPriceL.Text, "VND");
            txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceM.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");
        }

        private void txtTienCong_Leave(object sender, EventArgs e)
        {
            CommitValue();
            txtTienCong.Text = utcommand.FormatMoney(txtTienCong.Text, "VND");
            txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceM.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) + float.Parse(utcommand.FormatString(txtTienCong.Text))), "VND");

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(cmbNV.SelectedValue.ToString());
        }









    }
}